package com.graduate.cec.controller;


import com.graduate.cec.common.JsonPage;
import com.graduate.cec.common.JsonResult;
import com.graduate.cec.pojo.dto.CarouselDTO;
import com.graduate.cec.pojo.dto.CarouselSimpleDTO;
import com.graduate.cec.pojo.vo.CarouselVO;
import com.graduate.cec.service.CarouselService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@RestController
@RequestMapping(value = "/carousel",produces = "application/json;charset=utf-8")
@Api(tags = "轮播图")
public class CarouselController {

    @Resource
    private CarouselService carouselService;

    /****************        增       ******************************************************************/
    @GetMapping("/insert-carousel")
    @ApiOperation("增加分类")
    public JsonResult<CarouselVO> insertCarousel(CarouselDTO carouselDTO){
        carouselService.insertCarousel(carouselDTO);
        return JsonResult.ok("插入成功");
    }

    //上传图片
    @PostMapping("/upload-picture")
    @ApiOperation("上传图片")
    public JsonResult<String> upload(MultipartFile picFile) throws Exception {
        String fileName = carouselService.uploadPictures(picFile);
        return JsonResult.ok("插入成功",fileName);

    }

    /****************        删       ******************************************************************/
    @DeleteMapping("/delete-by-keys/{id}")
    @ApiOperation("通过主键删除分类")
    public JsonResult<Void> deleteByKeys(@PathVariable Integer id){
        log.info("ids:{}", id);
        carouselService.deleteByKeys(id);
        return JsonResult.ok("删除成功");
    }

    //删除上传的图片
    @GetMapping("/remove-picture")
    @ApiOperation("删除图片")
    public JsonResult<Void> remove(String pictureName) {
        carouselService.removePicture(pictureName);
        return JsonResult.ok("删除成功");
    }



    /****************        改       ******************************************************************/
    @PostMapping("/update-by-key")
    @ApiOperation("通过主键修改分类内容")
    public JsonResult<Void> updateByKey(@RequestBody CarouselDTO carouselDTO){
        log.info("carouselDTO:{}",carouselDTO);
        carouselService.updateByKey(carouselDTO);
        return JsonResult.ok("修改成功");
    }


    /****************        查        ******************************************************************/
    /**
     * 根据查询条件查找列表
     * @param carouselSimpleDTO 条件
     * @return JsonResult<JsonPage<CarouselVO>>
     */
    @GetMapping("/get-list-by-example")
    @ApiOperation("通过一定条件(姓名)查找列表")
    public JsonResult<JsonPage<CarouselVO>> getListByExample(CarouselSimpleDTO carouselSimpleDTO){
        log.info("carouselDTO:{}", carouselSimpleDTO);
        JsonPage<CarouselVO> jsonPage = carouselService.getListByExample(carouselSimpleDTO);
        return JsonResult.ok("查询成功",jsonPage);
    }

    /**
     * 查询所有列表
     * @return JsonResult<List<CarouselVO>>
     */
    @GetMapping("/get-all-list")
    @ApiOperation("查询全部列表")
    public JsonResult<List<CarouselVO>> getAllList(){
        return JsonResult.ok("查询成功",carouselService.getAllList());
    }


}
